﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HolidayDataAccessLayer;
using System.Globalization;
namespace HolidayWeb
{
    public partial class ListTours : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HolidayDataAccess dataLayer = new HolidayDataAccess();
            IQueryable<Destination> destinations = dataLayer.FindDestination(null, null, null, null, null, null);
            
            ToursRepeater.DataSource = destinations;
            ToursRepeater.DataBind();
          // toursDiv.InnerText = destinations.Count.ToString();
            //foreach (var item in destinations)
            //{
            //    toursDiv.InnerText += item.DestinationName + "<br/>";
            //}
        }

        protected void FilterButton_Click(object sender, EventArgs e)
        {
            HolidayDataAccess dataLayer = new HolidayDataAccess();
            string destFilter = null;
            if (DestinationBox.Text.Length > 0) destFilter = DestinationBox.Text;
            decimal? priceFrom = null;
            decimal tmp = 0;
            if (decimal.TryParse(PriceFromBox.Text, out tmp))
            {
                priceFrom = tmp;
            }

            decimal? priceTo = null;
            
            if (decimal.TryParse(PriceToBox.Text, out tmp))
            {
                priceTo = tmp;
            }

            DateTime? dateFrom = null;
            DateTime tmpDate;
            if (DateTime.TryParseExact(DateFromBox.Text, new string[] { "dd MM yyyy", "dd.MM.yyyy", "MM/dd/yyyy" }, 
                CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out tmpDate))
            {
                dateFrom = tmpDate;
            }
          //  Response.Write(tmpDate.ToString());
            DateTime? dateTo = null;
           
            if (DateTime.TryParseExact(DateToBox.Text, new string[] { "dd MM yyyy", "dd.MM.yyyy", "MM/dd/yyyy" }, 
                CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out tmpDate))
            {
                dateTo = tmpDate;
            }
           // Response.Write(tmpDate.ToString());
            IQueryable<Destination> destinations = dataLayer.FindDestination(destFilter, priceFrom, priceTo,dateFrom, dateTo, null);

            ToursRepeater.DataSource = destinations;
            ToursRepeater.DataBind();
        }
    }
}